package com.zj.authorizationserver.service.impl;

import com.zj.authorizationserver.dto.AdminUserDetails;
import com.zj.authorizationserver.mbg.model.UmsAdmin;
import com.zj.authorizationserver.mbg.model.UmsPermission;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * SpringSecurity定义的核心接口，用于根据用户名获取用户信息
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UmsAdminServiceImpl adminService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //获取登录用户信息
        UmsAdmin admin = adminService.getAdminByUsername(username);
        if (admin != null) {
            List<UmsPermission> permissionList = adminService.getPermissionList(admin.getId());
            return new AdminUserDetails(admin,permissionList);
        }
        throw new UsernameNotFoundException("用户名或密码错误");
    }
}
